package com.czyxy.services;


import com.czyxy.domain.StudentFee;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * Created by mike on 6/8/17.
 */
@Service
public class JsonService {
    public String saveJsonFile(List<? extends Object> datalist,String path) throws IOException {
        File file = new File(path);
        file.createNewFile();
        ObjectMapper mapper=new ObjectMapper();
        mapper.writeValue(file,datalist);
        return file.getAbsolutePath();

    }
    public String saveJsonFile(List<? extends Object> datalist) throws IOException {
        return saveJsonFile(datalist,"datalist.json");
    }
    public List<StudentFee> readJsonFile(String path) throws IOException {

        File file = new File(path);
        ObjectMapper mapper=new ObjectMapper();
        List<StudentFee> list = mapper.readValue(file,List.class);
        return list;
    }
    public List<StudentFee> readJsonFile(File file) throws IOException {

        ObjectMapper mapper=new ObjectMapper();
//        List<StudentFee> list = mapper.readValue(file,List.class);
        List<StudentFee> list = mapper.readValue(file,new TypeReference<List<StudentFee>>(){});
        System.out.println(list.get(0));
        return list;
    }
}
